Test Case
Overview
This crate provides #[test_case]
procedural macro attribute that generates multiple parametrized tests using one body with different input parameters.
A test is generated for each data set passed in test_case
attribute.
Under the hood, all test cases that share same body are grouped into mod
, giving clear and readable test results.
Getting Started
First of all you have to add this dependency to your Cargo.toml
:
[]
= "1.2.3"
Additionally, you have to import the procedural macro with use
statement:
use test_case;
Example usage:
// The next two lines are not needed for 2018 edition or newer
extern crate test_case;
Output from cargo test
for this example:
; ; ; ;
Examples
If your only assertion is just assert_eq!
, you can pass the expectation as macro attribute using =>
syntax:
Which is equivalent to
Attributes and expectation may be any expresion unless they contain =>
, e.g.
Note: in fact, =>
is not prohibited, but the parser will always treat last =>
sign as beginning of expectation definition.
Test case names are optional. They are set using ;
followed by string literal at the end of macro attributes.
Example generated code:
Modifiers
inconclusive
Context ignored test cases (deprecated, will be dropped in 2.0.0)
If test case name (passed using ;
syntax described above) contains a word "inconclusive", generated test will be marked with #[ignore]
.
Keyword 'inconclusive'
If test expectation is preceded by keyword inconclusive
the test will be ignored as if it's description would contain word inconclusive
Generated code:
matches
If test expectation is preceded by matches
keyword, the result will be tested whether it fits within provided pattern.
panics
If test case expectation is preceded by panics
keyword and the expectation itself is &str
or expresion that evaluates to &str
then test case will be expected to panic during execution.
is|it (feature = "hamcrest_assertions")
This feature requires addition of hamcrest2 crate to your Cargo.toml:
= { = "1.1.0", = ["hamcrest_assertions"] }
= "0.3.0"
After that you can use test cases with new keywords is
and it
which will allow you to use hamcrest2 assertions (doc)
async in test cases
Test cases can work with tokio
, async-std
and other runtimes, provided #[test...]
attribute from mentioned libraries is used as a last attribute.
eg.
async
Porting from Rust #[test]
s with Result
types
It is important to note that test-case does not support the Rust 2018+ idiom of failing tests by returning a Result<T, E>
with an error type.
The simplest way to remedy this is to append ... => Ok(_)
to each #[test-case(...)
expression, e.g:
Previously, tests relying on the return error being checked would silently pass; as of 1.2.2 attempting to return a Result<>
without an appropriate return check in the expression will result in a compilation error. However if you wish to keep the old behaviour for some reason the feature flag allow_result
will disable the check.
License
Licensed under of MIT license (LICENSE-MIT or https://opensource.org/licenses/MIT)
Contribution
All contributions and comments are more than welcome! Don't be afraid to open an issue or PR whenever you find a bug or have an idea to improve this crate.
Recommended tools:
cargo readme
- to regenerate README.md based on template and lib.rs commentscargo insta
- to review test snapshotscargo edit
- to add/remove dependenciescargo fmt
- to format codecargo clippy
- for all insights and tipscargo fix
- for fixing warnings